package com.ht.module.sys.controller;

import com.ht.config.log.annotation.SysLog;
import com.ht.constant.DicConstant;
import com.ht.module.sys.entity.SysDept;
import com.ht.module.sys.service.SysDeptService;
import com.ht.util.CommMethod;
import com.ht.util.JsonResult;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;



/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author hejialun
 * @since 2022-04-18
 */
@Tag(name = "部门管理 ")
@RestController
@RequestMapping("/sys-dept")
public class SysDeptController {

    
    @Autowired
    private SysDeptService sysDeptService;


    @Operation(summary = "获取部门树携带数据权限")
    @GetMapping("/findTreeAuth")
    @SysLog(msg = "携带权限查询部门树")
    public JsonResult findTreeAuth(SysDept en){
        return JsonResult.success(sysDeptService.findTreeAuth(en));
    }



    @Operation(summary = "获取部门树")
    @GetMapping("/findTree")
    @SysLog(msg = "获取部门树")
    public JsonResult findTree(SysDept en){
        return JsonResult.success(sysDeptService.findTree(en));
    }




    @Operation(summary = "新增")
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('SYS_DEPT_ADD')")
    @SysLog(msg = "新增部门",type = DicConstant.OperLogType.ADD)
    public JsonResult add(@RequestBody @Valid SysDept en){
        CommMethod.beanCreateNoDept(en);
        sysDeptService.add(en);
        return JsonResult.success();
    }


    @Operation(summary = "通过id删除")
    @DeleteMapping("/delete-by-id/{id}")
    @PreAuthorize("hasAnyAuthority('SYS_DEPT_DEL')")
    @SysLog(msg = "通过id删除部门",type = DicConstant.OperLogType.DEL)
    public JsonResult delete(@PathVariable(value = "id") String id){
        sysDeptService.delById(id);
        return JsonResult.success();
    }


    @Operation(summary = "修改")
    @PutMapping("/update")
    @PreAuthorize("hasAnyAuthority('SYS_DEPT_UPDATE')")
    @SysLog(msg = "修改部门",type = DicConstant.OperLogType.UPDATE)
    public JsonResult updateById(@RequestBody @Valid SysDept en){
	    CommMethod.beanUpdate(en);
        sysDeptService.editById(en);
        return JsonResult.success();
    }

}
